/*Objective_C Implementation of the RPNCalculator Class: RPNCalculator.m*/

#import "RPNCalculator.h"

@implementation RPNCalculator : Object

//Initialize the displays by clearing them
-appDidInit:sender
{
	[self clearEntry];
	[display setFloatValue:0.0];
	return self;
}
 
 //Initialize a new RPNCalculator instance
-init
{
	stack = [[Stack alloc] init];
	return [super init];
}	
																   
// Enter a number - get it from the entry field
- enter:sender
{
	[stack push:[entryField floatValue]];
	[display setFloatValue: [stack top]];
	[self clearEntry];
	return self;
}

//Add top values on stack and display result
-add:sender{
	[display setFloatValue: [self add]];
	[self clearEntry];
	return self;
}

//Add two elements
-(float)add{
	[stack push: ( [stack  pop] +  [stack  pop]) ];
	return [stack top];
}

//Subtract top values on stack and display result
-subtract:sender{
	[display setFloatValue: [self subtract]];
	[self clearEntry];
	return self;
}

//Subtract two elements
-(float)subtract{
	[stack push: ( -[stack  pop] +  [stack  pop]) ];
	return [stack top];
}

// Print the calculator's stack
-printStack{
	[stack printStack];
	return self;
}

// Clear the entry field
-clearEntry
{
	[entryField setStringValue:""];
	[entryField selectText:self];	//select entry again
	return self;
}

// Empty the calculator's stack
-allClear:sender
{
	[stack empty];
	[display setFloatValue: [stack top]];
	[self clearEntry];
	return self;
}

// Free the calculator and its  stack
-free
{
	[stack free];
	return [super free];
}

@end
